package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.CourseDao;
import com.gdit.zax.entitys.BaseEntity;
import com.gdit.zax.entitys.Comment;
import com.gdit.zax.entitys.Course;
import com.gdit.zax.utils.DBUtil;

public class CourseDaoImpl implements CourseDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public CourseDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public ArrayList<Course> getAllCourse() {
		ArrayList<Course> list = new ArrayList<Course>();
		String sql = "select * from t_course";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			while(rs.next()){
				Course be = DBUtil.getobject(rs, Course.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<Course> getAllCourse(int uid) {
		ArrayList<Course> list = new ArrayList<Course>();
		String sql = "select * from t_course where anthorid=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Course be = DBUtil.getobject(rs, Course.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<BaseEntity> getAllCoursetype() {
		ArrayList<BaseEntity> list = new ArrayList<BaseEntity>();
		String sql = "select * from t_coursetype";
		
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			while(rs.next()){
				BaseEntity be = DBUtil.getobject(rs, BaseEntity.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public Course getCourseByName(String name) {
		String sql = "select * from t_course where Name=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, Course.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public BaseEntity addCourseTypeWhetherExist(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getCourseScount(int cid) {
		String sql = "select CourseId,count(CourseId) as scount from t_seerecord as sc where CourseId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			rs = ps.executeQuery();
			
			if(rs.next()){
				int count = rs.getInt(2);
				return count;
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int getCourseCcount(int cid) {
		String sql = "select CourseId,count(CourseId) as ccount from t_collect as sc where CourseId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			rs = ps.executeQuery();
			
			if(rs.next()){
				int count = rs.getInt(2);
				return count;
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public ArrayList<Comment> listAllComment(int page, int onepagecount) {
		String sql = "select * from t_comment  WHERE ReCommentId=-1 limit ?,?";
		ArrayList<Comment> list = new ArrayList<Comment>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				Comment com = DBUtil.getobject(rs, Comment.class);
				list.add(com);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<Comment> listComment(int cid, int page, int onepagecount) {
		String sql = "select * from t_comment  WHERE CourseId = ? limit ?,?";
		ArrayList<Comment> list = new ArrayList<Comment>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				Comment com = DBUtil.getobject(rs, Comment.class);
				list.add(com);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public ArrayList<Comment> listReComment(int commentid, int page,
			int onepagecount) {
		return null;
	}

	public ArrayList<Comment> listSentComment(int cid, int page,
			int onepagecount) {
		String sql = "select * from t_comment  WHERE ReCommentId=-1 and CourseId = ? limit ?,?";
		ArrayList<Comment> list = new ArrayList<Comment>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				Comment com = DBUtil.getobject(rs, Comment.class);
				list.add(com);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	
	public ArrayList<Course> getAllCourse(int uid, int page, int onepagecount) {
		ArrayList<Course> list = new ArrayList<Course>();
		String sql = "select * from t_course where anthorid=? limit ?,?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page-1) * page);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Course be = DBUtil.getobject(rs, Course.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean changeDynamic(int cid,String content) {
		String sql = "update t_course set Dynamic=? where Id=?";	
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, content);
			ps.setInt(2, cid);
			int re = ps.executeUpdate();
			if (re!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public Course getCourseById(int id) {
		String sql = "select * from t_course where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, Course.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
